//
//  ProgromViewController.m
//  QinrenManagers
//
//  Created by easaa on 15/8/7.
//  Copyright (c) 2015年 easaa. All rights reserved.
//

#import "ProgromViewController.h"
#import "SendProgramViewController.h"

#import "ProductListModel.h"

@interface ProgromViewController ()<UITableViewDataSource,UITableViewDelegate>

/**解决方案数据源*/
@property (nonatomic, strong) NSMutableArray * schemeDataSource;

@property (nonatomic, strong) UITableView * baseTableView;

@end

@implementation ProgromViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        _schemeDataSource = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self requestForData];
    [self creatTableView];

}

#pragma mark --------- 网络请求-------------
- (void)requestForData
{
    
    EAHttpAPIClient *manager = [EAHttpAPIClient manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *parameters = @{@"customer_id":self.customer_id,@"pagesize":@"200",@"pageindex":@"1",@"time":@"0"};
    NSLog(@"%@",parameters);
    [manager GetWithParameters:parameters method:@"search.solution.list" success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *resposeString ;
        if ([responseObject isKindOfClass:[NSData class]]) {
            resposeString = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        }else
            resposeString = responseObject;
        
        NSString *jsonString = [DataParser parseString:resposeString];
        id dictionary = [JSONKit jsonToArrayOrDictionary:jsonString];
        NSLog(@"客户解决方案的dictonary :%@",[dictionary description]);
        
        
        if ([[dictionary objectForKey:@"verification"]intValue] == 1)
        {
        
            for (NSDictionary *dataDic in dictionary[@"data"]) {
                
                ProductListModel *productListModel = [[ProductListModel alloc]init];
                [productListModel setValuesForKeysWithDictionary:dataDic];
                [_schemeDataSource addObject:productListModel];
                
            }
            
            [_baseTableView reloadData];
            _baseTableView.legendFooter.hidden = NO;
            
            
        }
        else
        {
            
            NSString *error = [dictionary objectForKey:@"error"];
            [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
            [SVProgressHUD showErrorWithStatus:error];
        }
        
        [_baseTableView.footer endRefreshing];
        [_baseTableView.header endRefreshing];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
        [SVProgressHUD showErrorWithStatus:[error localizedDescription]];
        [_baseTableView.footer endRefreshing];
        [_baseTableView.header endRefreshing];
        
    }];
    
}


- (void)creatTableView
{
    _baseTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H) style:UITableViewStylePlain];
    
    _baseTableView.delegate = self;
    _baseTableView.dataSource = self;
    
    _baseTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [self.view addSubview:_baseTableView];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _schemeDataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    static NSString *CellIdentifier1 = @"schemeCell";
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //显示最右边的箭头
    
    //建模填充数据
    ProductListModel * productListModel = _schemeDataSource[indexPath.row];
    
    [cell.imageView setImage:[UIImage imageNamed:@"guanlifangan_"]];
    cell.textLabel.text = productListModel.solution_name;
    
    return cell;
    
}

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SendProgramViewController *sendProgramViewController = [[SendProgramViewController alloc] init];
    
    ProductListModel * productListModel = _schemeDataSource[indexPath.row];
    
    sendProgramViewController.title = @"方案分类";
    sendProgramViewController.customer_id = self.customer_id;
    sendProgramViewController.hx_username = self.hx_username;
    sendProgramViewController.solution_id = productListModel.solution_id;
    sendProgramViewController.navType = self.navType;
    [self.navigationController pushViewController:sendProgramViewController animated:NO];
   
}

@end
